package me.fangx.zhihu.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import me.fangx.zhihu.R;


/**
 * 作者： Owen
 * 时间： 2016/6/20 14:08
 * 类说明：
 */
public class EmptyView extends CustomBaseViewRelative{

    private LinearLayout ll_empty;
    private ImageView iv_empty;
    private TextView tv_empty;
    private TextView tv_retry;
    private RetryListener retryListener;
    private ProgressBar mProgressBar;

    public EmptyView(Context context) {
        super(context);
    }

    public EmptyView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.view_empty;
    }

    @Override
    protected void initView() {
        ll_empty = (LinearLayout) findViewById(R.id.ll_empty);
        iv_empty = (ImageView) findViewById(R.id.iv_empty);
        tv_empty = (TextView) findViewById(R.id.tv_empty);
        tv_retry = (TextView) findViewById(R.id.tv_retry);
        mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
    }

    private void initListener(){
        ll_empty.setOnClickListener(retry);
        tv_retry.setOnClickListener(retry);
    }

    View.OnClickListener retry = new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            if (retryListener!=null){
                retryListener.retry();
            }
        }
    };

    public void setEmptyImage(int resId){
        iv_empty.setImageResource(resId);
    }

    public void setEmptyImageLayoutParams(ViewGroup.LayoutParams params){
        iv_empty.setLayoutParams(params);
    }

    public void setEmptyText(String text){
        tv_empty.setText(text);
    }

    public void setEmptyRetryVisiable(boolean isVisiable){
        tv_retry.setVisibility(isVisiable? View.VISIBLE:View.GONE);
    }

    public void setEmptyRetryText(String text){
        tv_retry.setText(text);
    }

    public interface RetryListener {
        void retry();
    }

    public void setRetryListener(RetryListener retryListener){
        this.retryListener = retryListener;
        initListener();
    }

    public void hide(){
        this.setVisibility(View.GONE);
    }

    public void show(){
        this.setVisibility(View.VISIBLE);
    }

    public void showProgress(){
        mProgressBar.setVisibility(View.VISIBLE);
        ll_empty.setVisibility(View.GONE);
    }

    public void hideProgress(){
        mProgressBar.setVisibility(View.GONE);
        ll_empty.setVisibility(View.VISIBLE);
    }
}
